<template>
	<view class="page">
		<u-navbar title="摄像头" border @leftClick="leftClick" placeholder>
		</u-navbar>
		<view class="body">
			<template v-if='editShow'>
				<view class="title">
					请输入设备序列号
				</view>
				<view class="title2">
					在设备上查看设备序列号并在此输入
				</view>
				<u-input v-model="deviceSerial" placeholder="设备序列号,存在英文字母的设备序列号，字母需为大写"></u-input>
				<view style="height: 20rpx;"></view>
				<u-input v-model="validateCode" placeholder="设备验证码，设备机身上的六位大写字母"></u-input>
				<view style="height: 20rpx;"></view>
				<u-input v-model="name" placeholder="请输入设备名称"></u-input>
				<view style="height: 20rpx;"></view>
				<u-button color=" #3975c6" text="确认" @click='binding'
					:disabled="!deviceSerial||!validateCode||!name"></u-button>
			</template>
			<template v-else>
				<view>
					请选择录入设备的方式
				</view>
				<view class="bodyItem" @click='scanCode'>
					<u-icon name='scan' label='扫描二维码' size='50rpx' labelSize='30rpx'></u-icon>
					<u-icon name='arrow-right' size='40rpx'></u-icon>
				</view>
				<view class="bodyItem" @click="editShow=true">
					<u-icon name='edit-pen' label='手动录入' size='50rpx' labelSize='30rpx'></u-icon>
					<u-icon name='arrow-right' size='40rpx'></u-icon>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				editShow: false,
				deviceSerial: null,
				validateCode: null,
				name:null
			}
		},
		watch: {
			editShow() {
				this.deviceSerial = null
				this.validateCode = null
				this.name = null
			}
		},
		onLoad() {

		},
		methods: {
			scanCode() {
				wx.scanCode({
					success: (res) => {
						const arr = res.result.split('\r')
						if (!arr[1] || !arr[2]) {
							uni.$u.toast('二维码有误')
							return
						}
						this.editShow=true
						setTimeout(()=>{
							this.deviceSerial = arr[1]
							this.validateCode = arr[2]
						},100)
					},
					fail(err) {}
				})
			},
			leftClick() {
				if (this.editShow) {
					this.editShow = false
				} else {
					this.$utils.back()
				}
			},
			binding() {
				let obj = {
					serial_number: this.deviceSerial,
					validate_code: this.validateCode
				}
				// uni.setStorageSync('camera', {
				// 	deviceSerial:this.deviceSerial,
				// 	validateCode:this.validateCode
				// })
				// this.$utils.navigateTo({
				// 	url:'/camera/queryDevice'
				// })
				this.$api_company.Device.Add(obj).then(res=>{
					uni.$u.toast('绑定成功')
				}).catch(err=>{
					uni.$u.toast(err.message)
				})
			}
		}
	}
</script>

<style lang="scss">
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		margin-top: 20rpx;
	}

	.bodyItem {
		padding: 30rpx 20rpx;
		border-radius: 8rpx;
		border: 1rpx solid #bbb;
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
	}

	.title {
		font-size: 35rpx;
		margin-bottom: 20rpx;
		text-align: center;

	}

	.title2 {

		font-size: 28rpx;
		margin-bottom: 20rpx;
		text-align: center;
		color: #999;
	}
</style>